<?php
/**
 * Customers Classes Controller
 *
 * PHP 5.2+
 *
 * Copyright 2012
 * @date 21-April-2012
 * @author
 * @property Auth $Auth
 * @version 0.0.1
 **/
/**
* @property CustomerType $CustomerType
* @property Customer $Customer
* @property Interest $Interest
* @property CustomerInterest $CustomerInterest
* @property SendMailComponent $SendMail
* @property EmstrainingComponent $Emstraining
* @property Mobilecode $Mobilecode test
* @property CustomerShopperGroup $CustomerShopperGroup */

App::uses('File', 'Utility');
class CustomersController extends AppController 
{

    public $sN = 'Customer'; 
    public $pN = 'customer';
    public $lN = 'customers';    
    public $conditions = array();
    public $components = array("Emstraining","SendMail","Zend",'Captcha' => array(
                'rotate' => true
            ));
    //public $uses = array('Zend.Zend');
    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel("Customer");
        $this->set('sN', $this->sN);        
        $this->set('pN', $this->pN);
        $this->set('lN', $this->lN);        
        if(!$this->request->is("ajax")){
            if(!$this->Auth->user('id')&&$this->request->params['action']!="login"&&$this->request->params['action']!='register'){
                $this->Session->setFlash("You must to login first!");
                $this->redirect("/");
            }
        }
    }
    public function admin_index() {
        $this->loadModel("CustomerShopperGroup");
        $pagi = array();
        $customers =  $this->Customer->find("all");
        if(!empty($this->data)){
            $or = array();
            $and = array();
            if($this->data['Customer']['name']){
                $or['f_name like']="%{$this->data['Customer']['name']}%";
                $or['l_name like']="%{$this->data['Customer']['name']}%";
            }
            if($this->data['Customer']['mobile_number']){
                $and['mobile like']="%{$this->data['Customer']['mobile_number']}%";
            }
            if($this->data['Customer']['customer_type_id']){
                $and['customer_type_id']=$this->data['Customer']['customer_type_id'];
            }
            if($this->data['Customer']['shopper_group_id']){
                $cus = $this->CustomerShopperGroup->get_customers_by_group($this->data['Customer']['shopper_group_id']);
                $cus_arr = array();
                if(!empty($cus)){
                    foreach ($cus as $value) {
                        $cus_arr[]=$value['Customer']['id'];
                    }
                }
                $and['Customer.id']=$cus_arr;
            }
            if($this->data['Customer']['balance_from']){
                $and['point >=']=$this->data['Customer']['balance_from'];
            }
            if($this->data['Customer']['balance_to']){
                $and['point <=']=$this->data['Customer']['balance_to'];
            }
            $pagi = array('AND'=>$and,"OR"=>$or);
        }
        $this->Customer->unBindModel(array('belongsTo'=>array('Country','City')));
        $this->paginate = array(
            'limit' => 20,
            'conditions'=>$pagi
        );
        $values = $this->paginate($this->sN);
        
        $return = array();
        foreach ($values as $value) {
            $value['Customer']['shopper_group']=  $this->CustomerShopperGroup->show_groups_by_customer($value['Customer']['id']);
            $return[]=$value;
        }
        $this->set('values', $return);
        
        $this->loadModel("CustomerType");
        $this->loadModel("ShopperGroup");
        $this->set("customer_types", $this->CustomerType->get_list());
        $this->set("shopper_groups", $this->ShopperGroup->get_list());
    }
    
    public function admin_edit_type($id) {
        if(empty($this->data)) {
            $this->request->data = $this->Customer->read(null, $id);
        }else{
            $this->Customer->id = $id;
            if($this->Customer->save($this->data,false)) {
                $this->Session->setFlash(__('Updated Successfully', true));
                $this->redirect(array('controller' => 'customers', 'action' => 'index', 'admin' => true));
            }
        }
        $this->loadModel("CustomerType");
        $this->set("customer_types",  $this->CustomerType->get_list());
    }
    public function admin_delete(){
        
    }
    public function admin_edit_group($id){
        $this->loadModel("CustomerShopperGroup");
        if(!empty($this->data)) {
            if(is_array($this->data['CustomerShopperGroup']['shopper_group_id'])&&!empty($this->data['CustomerShopperGroup']['shopper_group_id'])){
                $this->CustomerShopperGroup->delete_groups_by_customer($id);
                foreach ($this->data['CustomerShopperGroup']['shopper_group_id'] as $value) {
                    $this->CustomerShopperGroup->create();
                    $this->CustomerShopperGroup->save(array('customer_id'=>$id,'shopper_group_id'=>$value));
                }
                $this->Session->setFlash(__('Updated Successfully', true));
                $this->redirect(array('controller' => 'customers', 'action' => 'index', 'admin' => true));
            }else{
                $invalidate_array['shopper_group_id']=array('0'=>"Please choose some shopper group for customer");
                $this->set('invalidate_array',$invalidate_array);
            }
        }
        $this->request->data = $this->Customer->read(null, $id);
        $selected_group =  $this->CustomerShopperGroup->show_groups_by_customer($id);
        //pr($selected_group);die;
        $selected = array();
        foreach ($selected_group as $value) {
            $selected[]=$value['id'];
        }
        //pr($selected);die;
        //$this->request->data['CustomerShopperGroup']['shopper_group_id']=  $selected;
        $this->loadModel("ShopperGroup");
        $this->set("groups",  $this->ShopperGroup->get_list());
        //$selected = implode(",", $selected);
        $this->set("selected_groups", $selected);
    }
    public function send_mobilecode() {
        $this->loadModel("Mobilecode");
        
        if($this->request->is('ajax')){
            $this->autoRender = false;
            $code = $this->Sms->create_code();
            if($_REQUEST['mobile']){
                $mobilecode = $this->Mobilecode->findBySessionId($this->Session->id());
                if(!empty($mobilecode)) {
                    $this->Mobilecode->id = $mobilecode['Mobilecode']['id'];
                    $this->Mobilecode->saveField('code',$code);
                }else{
                    $this->Mobilecode->create();
                    $this->Mobilecode->save(array('customer_id'=>  0,'code'=>$code,'session_id'=>$this->Session->id()));
                }
                //$this->send_sms($_REQUEST['mobile'], $code);
                echo 'send';
            }else{
                echo 'false';
            }
        }else{
            $code = $this->Sms->create_code();
            $customer = $this->Customer->findById($this->Auth->user('id'));
            $mobilecode = $this->Mobilecode->findByCustomerId($this->Auth->user('id'));
            if(!empty($mobilecode)) {
                $this->Mobilecode->id = $mobilecode['Mobilecode']['id'];
                $this->Mobilecode->saveField('code',$code);
            }else{
                $this->Mobilecode->create();
                $this->Mobilecode->save(array('customer_id'=>  $this->Auth->user("id"),'code'=>$code));
            }
            $this->send_sms($customer['Customer']['mobile'], $code);
            $this->Session->setFlash("The code has been sent to your phone. Please use it to verify your phone");
            $this->redirect(array('action'=>'index'));
        }
        
        
    }
    public function check_name($email=null){
        $this->autoRender = false;
        if($this->request->is("ajax")){
            $customer = $this->Customer->findByEmail($email);
            if(!empty($customer)){
                return $customer["Customer"]['f_name']." ".$customer["Customer"]['l_name'];
            }
        }
    }
    public function verify_code($code) {
        $this->loadModel("Mobilecode");
        
        if($this->request->is('ajax')){
            $this->autoRender = false;
            $mobilecode = $this->Mobilecode->findBySessionId($this->Session->id());
            if($mobilecode['Mobilecode']['code']==$code){
                echo "verify";
            }else{
                echo "false";
            }
        }else{
            $customer = $this->Customer->findById($this->Auth->user('id'));
            $mobilecode = $this->Mobilecode->findByCustomerId($this->Auth->user('id'));
            if($mobilecode['Mobilecode']['code']==$code){
                $this->Mobilecode->delete($mobilecode['Mobilecode']['id']);
                $this->Customer->id = $customer["Customer"]['id'];
                $this->Customer->saveField("is_verify_mobile", 1);
                $this->Session->setFlash("You phone has been verified");
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash("You have input the wrong code. Please try again!");
                $this->redirect(array('action'=>'index'));
            }
        }
        
        
    }
    public function index(){
        $customer = $this->Customer->read(null,  $this->Auth->user('id'));
        $this->loadModel("CustomerInterest");
        $interests = $this->CustomerInterest->get_interest_by_customer($this->Auth->user('id'));
        $int = array();
        foreach($interests as $interest){
            $int[]=$interest['Interest']['name'];
        }
        $customer['Customer']['interest']=  implode(" , ", $int);
        $this->loadModel("Mobilecode");
        $is_send = 0;
        $cus = $this->Mobilecode->findByCustomerId($this->Auth->user('id'));
        if(!empty($cus)){
            $is_send = 1;
        }
        $this->set("is_send",$is_send);
        $this->set("customer", $customer );
    }
    public function login() {
        $this->setCustomerLogin();
        if($this->request->is("post")){
            $customer = $this->Customer->find('first',array('conditions'=>array('OR'=>array('email'=>  $this->data['Customer']['username'],'customer_number'=>  $this->data['Customer']['username']),'password'=>  AuthComponent::password( $this->data['Customer']['password']))));
            if(!empty($customer)){
                if($customer['Customer']['status']==Customer::ACTIVE){
                    if ($this->Auth->login($customer['Customer'])) {
                        $this->Session->write("Auth.User.indentify","customer");
                        $this->redirect(array('controller' => 'customers', 'action' => 'index','admin'=>false));
                    } else {
                        $this->Session->setFlash(__('Invalid email or password, try again'));
                    }
                }else {
                    $this->Session->setFlash(__('Your account has been blocked ! Please contact with administrator'));
                }
            }else {
                $this->Session->setFlash(__('Invalid email or password, try again'));
            }
        }
    }
    public function logout() {
        $this->autoRender = false;
        $this->Session->destroy();
        //$this->Session->setFlash(__('Successfully logged out!', true));                		
        $this->redirect("/");
    }
    
    public function register(){
        if(!empty($this->data)){
            if($this->Captcha->getCode()==$this->data['Customer']['captcha']){
                if($this->data['Customer']['is_verify_mobile']==1){
                    $check = 1;
                    if($this->data['Customer']['customer_number']!="" ||$this->data['Customer']['card_number'] != ""){
                        $customer_number = $this->data['Customer']['customer_number'];
                        $card_number = $this->data['Customer']['card_number'];
                        $customer = $this->Customer->find_customer($customer_number, $card_number);
                        if(!empty($customer)){
                            $check=0;
                            $invalidate_array=array('0'=>"The customer with customer number and card number has been existed");
                            $this->set('invalidate_array',$invalidate_array);
                        }else{
                            $this->loadModel("Elvi");
                            $customer = $this->Elvi->find_customer($customer_number, $card_number);
                            if(empty($customer)){
                                $check=0;
                                $invalidate_array=array('0'=>"We can not found this customer ! please enter a right customer number and card");
                                $this->set('invalidate_array',$invalidate_array);
                            }else{
                                $money = 0;
                                $customers =  $this->Elvi->find_all_customer($customer_number, $card_number);
                                foreach ($customers as $value) {
                                    $money += $value['Elvi']['point_earned'];
                                }
                                $this->request->data['Customer']['point']=  floor($money);
                                $this->Elvi->set_as_read($customer_number, $card_number);
                            }
                        }
                    }else{
                        
                        $this->Emstraining->generate_customer_number($customer_number,$customer_number_barcode);
                        $this->request->data['Customer']['customer_number']=  $customer_number;
                        $this->request->data['Customer']['customer_number_barcode']=  $customer_number_barcode;
                        $this->request->data['Customer']['card_number']=  $this->Emstraining->generate_number(20);
                        $this->request->data['Customer']['point']=  0;
                    }

                    if($check==1){
                        $this->request->data['Customer']['balance']=  0;
                        $this->request->data['Customer']['created_by']=  0;
                        $this->request->data['Customer']['status'] = 1;
                        if($this->Customer->save($this->data)) {
                            $cusID = $this->Customer->id;

                            if(!empty($this->data['CustomerInterest']['interest_id'])){
                                $this->loadModel("CustomerInterest");
                                foreach ($this->data['CustomerInterest']['interest_id'] as $value) {
                                    $this->CustomerInterest->create();
                                    $this->CustomerInterest->save(array('customer_id'=>$cusID,'interest_id'=>$value));
                                }
                            }
                            $customer = $this->Customer->read(null,$cusID);
                            $this->loadModel("Message");
                            $values = array();
                            $values['CUSTOMER_NAME'] = $customer['Customer']['f_name']." ".$customer['Customer']['l_name'];
                            $this->print_qrcode($customer['Customer']['customer_number_barcode']);
                            $values['ATTACHMENT']=  $this->qrcode_path."{$customer['Customer']['customer_number_barcode']}.png";

                            $values['PRIORITY'] = 'SMS';
                            $values['MOBILE'] = $customer['Customer']['mobile'];

                            //$this->SendMail->notifications(Message::SEND_MAIL_REGISTER, $customer['Customer']['email'], "Register Successfull", $values);
                            $this->SendMail->send(Message::SEND_MAIL_REGISTER, $customer['Customer']['email'], "Register Successfull", $values);

                            if(isset($this->request->data['Customer']['point'])&&$this->request->data['Customer']['point']!=0){
                                $this->Session->setFlash("Register successful . {$this->request->data['Customer']['point']} point has been added to your account . Please login to check this!");                		
                            }else{
                                $this->Session->setFlash("Register successful");                		
                            }
                            $this->redirect(array('controller'=>'users','action'=>'verification'));
                        }
                    }
                }else{
                    $this->Customer->invalidate('mobile','Please verify this number');
                }
            }else{
                $invalidate_array=array('0'=>"Wrong catcha");
                $this->set('invalidate_catcha_array',$invalidate_array);
            }
        }
        $this->loadModel("Country");
        $this->loadModel("Interest");
        $this->set("interests",  $this->Interest->get_list());
        $this->set("countries",  $this->Country->get_list());
    }
    
    public function create_ticket($customer, $id){
        $this->loadModel('Ticket');
        $ticket['Ticket']['first_name'] = $customer['f_name'];
        $ticket['Ticket']['last_name'] = $customer['l_name'];
        $ticket['Ticket']['country_id'] = $customer['country_id'];
        $ticket['Ticket']['city_id'] = $customer['city_id'];
        $ticket['Ticket']['mobile'] = $customer['mobile'];
        $ticket['Ticket']['email'] = $customer['email'];
        $ticket['Ticket']['customer_id'] = $id;
        $this->Emstraining = $this->Components->load('Emstraining');
        $s_id = $this->Emstraining->rand_string("ticket");
        
        $ticket['Ticket']['key'] = $s_id;
        $ticket['Ticket']['sla'] = 1;
        $this->Ticket->create();
        $this->Ticket->save($ticket);
    }
    
    public function get_city_list($type='Customer',$country_id=null,$city_id=null){
        $this->autoLayout=false;
        $this->loadModel('City');
        $this->City->unBindModel(array('belongsTo'=>array('Country')));
        $cities = $this->City->find('list',array('fields'=>'name','conditions'=>array('City.country_id'=>$country_id)));
        if(!$city_id){
            $city_id=0;
        }
        $this->set(compact('cities','city_id','type'));
    }
    
    public function edit($customer_id = null) {
        $this->loadModel("CustomerInterest");
        if(empty($this->data)) {
            $this->request->data = $this->Customer->read(null, $this->Auth->user('id'));
        }else{
            $this->Customer->id = $this->Auth->user('id');
            $old_phone = $this->Customer->field("mobile");
            if(!empty($this->data['CustomerInterest']['interest_id'])){
                $this->CustomerInterest->delete_interest_by_customer($this->Auth->user('id'));
                foreach ($this->data['CustomerInterest']['interest_id'] as $value) {
                    $this->CustomerInterest->create();
                    $this->CustomerInterest->save(array('customer_id'=>$this->Auth->user('id'),'interest_id'=>$value));
                }
            }
            if($this->Customer->save($this->data['Customer'],false)) {
                //if($old_phone != $this->data['Customer']['mobile']){
                //    $this->Customer->saveField("is_verify_mobile", 0);
                //    $this->Session->setFlash('Updated Successfully <br> You have changed your phone number . Please verify it');
                //}else{
                //    $this->Session->setFlash(__('Updated Successfully', true));
                //}
                
                $this->redirect(array('controller' => 'customers', 'action' => 'index', 'admin' => false));
            }
        }
        $this->loadModel("Country");
        $this->loadModel("Interest");
        $this->CustomerInterest->unbindModel(array('belongsTo'=>array('Customer','Interest')));
        $interested_ids = array();
        $intestests = $this->CustomerInterest->find('all',array('conditions'=>array('customer_id'=>  $this->Auth->user('id'))));
        foreach ($intestests as $value) {
            $interested_ids[]=$value['CustomerInterest']['interest_id'];
        }
        $this->set("interested",$interested_ids);
        $this->set("interests",  $this->Interest->get_list());
        $this->set("countries",  $this->Country->get_list());
    }
    public function admin_inactive($customer_id=null) {
        if(!$customer_id){
            $this->Session->setFlash("Invalid Customer Id");
            $this->redirect(array('controller'=>'customers','action'=>'index'));
        }
        $this->Customer->id = $customer_id;
        $this->Customer->saveField("status", Customer::INACTIVE);
        $this->Session->setFlash("The customer has been disabled");
        $this->redirect(array('controller'=>'customers','action'=>'index'));
    }
    public function admin_active($customer_id=null) {
        if(!$customer_id){
            $this->Session->setFlash("Invalid Customer Id");
            $this->redirect(array('controller'=>'customers','action'=>'index'));
        }
        $this->Customer->id = $customer_id;
        $this->Customer->saveField("status", Customer::ACTIVE);
        $this->Session->setFlash("The customer has been actived");
        $this->redirect(array('controller'=>'customers','action'=>'index'));
    }
    public function changepass($id=null){
        if(empty($this->data)) {
            $this->request->data = $this->Customer->read(null, $this->Auth->user('id'));
        } else {
            $this->Customer->id = $this->Auth->user('id');
            if($this->Customer->save($this->data)) {
                $this->Session->setFlash(__('Updated Successfully', true));
                $this->redirect(array('controller' => 'customers', 'action' => 'index', 'admin' => false));
            }
        }
    }
    public function purchase_hystory() {
        $this->loadModel("Elvi");
        
        $purchases = $this->paginate("Elvi",array('is_read'=>1,'customer_number'=>$this->Auth->user("customer_number"),'card_number'=>$this->Auth->user("card_number")));
        $purchases  = Set::sort($purchases , '{n}.Elvi.date_short', 'desc');
        $this->set("values", $purchases);
    }
    public function transfer_hystory() {
        $this->loadModel("Transfer");
        $user_id= $this->Auth->user("id");
        $values = $this->paginate("Transfer",array("Or"=>array('from_user'=>$user_id,'to_user'=>$user_id)));
        $values = Set::sort($values, '{n}.Transfer.created', 'desc');
        $this->set("values",$values);
    }
    public function transfer() {
        $this->loadModel("Transfer");
        if(!empty($this->data)){
            $customer = $this->Customer->read(null,  $this->Auth->user('id'));
            $friend = $this->Customer->findByEmail($this->data['Customer']["email"]);
            $invalidate_array = array();
            if(!empty($friend)&&$customer['Customer']['id']!=$friend['Customer']['id']){
                $balance = $this->data['Transfer']["point"];
                if($balance > $customer['Customer']['point']){
                    $invalidate_array['point']=array('0'=>"Please enter right balance point will be transfer");
                }else{
                    $this->Customer->id = $customer['Customer']['id'];
                    //$this->Customer->saveField('balance',$customer['Customer']['balance'] - $balance);
                    $this->Customer->saveField('point',$customer['Customer']['point'] - $balance);
                    
                    $this->Customer->id = $friend['Customer']['id'];
                    $this->Customer->saveField('point',$friend['Customer']['point'] + $balance);
                    $this->_checkBalance($friend['Customer']['id']);
                    
                    $this->Transfer->create();
                    $this->Transfer->save(array('from_user'=>  $this->Auth->user("id"),"to_user"=>$friend['Customer']['id'],'point'=>$balance,'reason'=>  $this->data['Transfer']['reason'],'created'=>date("Y-m-d H:i:s")));
                    
                    $this->Session->setFlash("You have transfered $balance point to your friend : {$friend['Customer']['email']}");
                    $this->redirect(array('controller' => 'customers', 'action' => 'index', 'admin' => false));
            
                }
            }else{
                $invalidate_array['email']=array('0'=>"Please enter right email of your friend!");
            }
            $this->set("invalidate_array",$invalidate_array);
        }
    }
    function _checkBalance($customer_id) {
        $this->loadModel("Setting");
        $this->loadModel("Voucher");
        $this->loadModel("Message");
        $customer = $this->Customer->read(null,$customer_id);
        if($customer['Customer']['point']>=1000){
            //$this->_send_voucher($customer_id, Setting::get_option("voucher_point"),Message::SEND_VOUCHER_WHEN_REACH_1000);
            //$balance = $customer['Customer']['point']-1000;
            //$this->Customer->id = $customer['Customer']['id'];
            //$this->Customer->saveField("balance", $balance);
            $this->SendMail->send_mail(Message::SEND_VOUCHER_WHEN_REACH_1000,$customer['Customer']['email'],"Balance reached",array('CUSTOMER_NAME'=>$customer['Customer']['f_name']." ".$customer['Customer']['l_name']));
        }
    }
    function _send_voucher($customer_id,$value,$type,$date=""){
        $customer = $this->Customer->read(null,$customer_id);
        $voucher_id = $this->_createVoucher($customer_id,$value,$date);
        $voucher = $this->Voucher->read(null,$voucher_id);
        $this->print_voucher($voucher_id);
        $values=array();
        $values['CUSTOMER_NAME']=$customer['Customer']['f_name']." ".$customer['Customer']['l_name'];
        $values['ATTACHMENT']=$this->vouchers_path."{$voucher['Voucher']['code']}.pdf";
        $this->SendMail->send_voucher($type, "en", $customer['Customer']['email'], $values);
        return $voucher_id;
    }
    function _createVoucher($customer_id,$value,$date=""){
        $this->loadModel("Voucher");
        $this->Voucher->create();
        if(!$date){
            $date = "0000-00-00";
        }
        $this->Voucher->save(array(
            'code'=>  $this->Emstraining->loyalty_voucher_number(),
            'value'=>$value,
            'expired_date'=>$date,
            'created'=>date("Y-m-d H:i:s"),
            'created_by'=>1,
            'customer_id'  =>$customer_id,
            'status'=>  Voucher::SENT_CUSTOMER,
            'active'=>1
        ));
        return $this->Voucher->id;
    }
    function admin_send_voucher($customers=null) {
        $this->loadModel("Message");
        $this->loadModel("Voucher");
        if(!empty($this->data)){
            $invalidate_array = array();
            if(empty($this->data['Customer']['customers'])){
                $invalidate_array['customers']=array('0'=>"Please choose some customer to send voucher");
            }
            if(!$this->data['Customer']['value']){
                $invalidate_array['value']=array('0'=>"Please enter the value of voucher will be sent for customer");
            }
            $date = $this->data['Voucher']['expired_date']['year']."-".$this->data['Voucher']['expired_date']['month'].'-'.$this->data['Voucher']['expired_date']['day'];
            if(!empty($invalidate_array)){
                $this->set('invalidate_array',$invalidate_array);
            }else{
                foreach ($this->data['Customer']['customers'] as $value) {
                    $this->_send_voucher($value, $this->data['Customer']['value'], Message::SEND_VOUCHER,$date);
                }
                $this->Session->setFlash("The voucher has been sent for customers");
                $this->redirect(array('action'=>'index'));
            }
        }else{
            $this->request->data['Customer']['customers']=  explode(",", $customers);
        }
        $this->set("customers",  $this->Customer->get_list());
    }
    function download_qrcode() {
        $this->autoRender = false;
        $customer_id = $this->Auth->user("id");
        $customer = $this->Customer->read(null,$customer_id);
        header("Content-Type: image/png");
        header("Content-Disposition: attachment; filename=\"{$customer['Customer']['customer_number']}.png\"");
        $this->Zend->writeQrcode($this->qrcode_path."{$customer['Customer']['customer_number_barcode']}.png",$customer['Customer']['customer_number_barcode'],"I");
    }
    function admin_send_customer($voucher_id=null) {
        $this->loadModel("Voucher");
        $this->loadModel("Message");
        $voucher = $this->Voucher->read(null,$voucher_id);
        if(!empty($this->data)){
            $customer_id = $this->data['Customer']['customer_id'];
            $customer = $this->Customer->read(null,$customer_id);
            $this->Voucher->id = $voucher_id;
            $this->Voucher->saveField('customer_id',$customer_id);
            $this->print_voucher($voucher_id);
            $values=array();
            $values['CUSTOMER_NAME']=$customer['Customer']['f_name']." ".$customer['Customer']['l_name'];
            $values['ATTACHMENT']=$this->vouchers_path."{$voucher['Voucher']['code']}.pdf";
            $this->SendMail->send_voucher(Message::SEND_VOUCHER, "en", $customer['Customer']['email'], $values);
            $this->Session->setFlash("The voucher has been sent to ".$customer['Customer']['f_name']." ".$customer['Customer']['l_name']);
            $this->redirect(array('controller'=>'vouchers','action'=>'index'));
        }
        $this->set("voucher",$voucher);
        $this->set("voucher_id",$voucher_id);
        $this->set("customers",  $this->Customer->get_list());
    }
    public function admin_view($id=null){
        if(!$id){
            $this->Session->setFlash('Invalid Id');
            $this->redirect(array('controller'=>'customers','action'=>'index'));
        }
        $customer = $this->Customer->read(null,$id);
        $this->loadModel("CustomerInterest");
        $interests = $this->CustomerInterest->get_interest_by_customer($this->Auth->user('id'));
        $int = array();
        foreach($interests as $interest){
            $int[]=$interest['Interest']['name'];
        }
        $customer['Customer']['interest']=  implode(" , ", $int);
        $this->set('customer', $customer );
    }
    
    public function captcha()  {
        $this->autoRender = false;
        $this->Captcha->generate();
    }
    public function redeem(){
        $customer = $this->Customer->read(null,  $this->Auth->user('id'));
        $this->set('customer',$customer);
    }
    public function redeem_point(){
        $this->loadModel("Redeem");
        $this->loadModel('Message');
        $customer = $this->Customer->read(null,  $this->Auth->user('id'));
        if($customer['Customer']['point']>=1000){
            //$voucher_id = $this->_send_voucher($customer['Customer']['id'], 30, Message::SEND_VOUCHER);
            $this->Redeem->create();
            $this->Redeem->save(array('customer_id'=>  $this->Auth->user('id'),'values'=>30,'point'=>1000,'voucher_id'=>0,'created'=>date('Y-m-d H:i:s')));
            $this->Customer->id = $this->Auth->user('id');
            $remain_point = $customer['Customer']['point'] - 1000;
            $this->Customer->saveField('point',$remain_point);
            $balance = (int)$customer['Customer']['point'] +30;
            $this->Customer->saveField('balance',$balance);
            $this->Session->setFlash("$30 has been added to your balance");
            $this->redirect(array('action'=>"redeemed"));
        }else{
            $this->Session->setFlash("You are not enough point to redeem");
            $this->redirect(array('action'=>"redeem"));
        }
    }
    public function redeemed() {
        $this->loadModel('Redeem');
        $list = $this->Redeem->find('all',array('customer_id'=>  $this->Auth->user('id')));
        $this->set('values',$list);
    }
}